package com.timoon.webcomponent.utility.product.linepic;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.timoon.sample.dao.model.Product;
import com.timoon.webcomponent.utility.product.BaseProductPanel;
import com.timoon.webcomponent.utility.product.linepic.component.SingleSmallPicProductPanel;

/**
 *<p>横向产品图片列表</p>
 *<p></p>
 *<p>Copyright:Copyright(c)2013</p>
 *<p>Company:timoon</p>
 *@author 付乐天
 *@version 1.0 */

public class HorizontalListPicProductPanel extends BaseProductPanel {

	@SpringBean(name="com.timoon.webcomponent.utility.product.service.ProductService")
	private HorizontalList4PicProductPanelService service;
	
	private Class<? extends WebPage> targetPage;
	
	private List<Product> list;
	
	private static Integer MAX=4;//横向商品个数
	
	private String title="DEFAULT";
	
	/**
	 * 
	 * @param id
	 * @param type  product.type
	 */
	public HorizontalListPicProductPanel(String id,Integer type,Class<? extends WebPage> targetPage) {
		super(id);
		// TODO Auto-generated constructor stub
		this.list=this.service.getProductByType(type);
		if(list.size()>HorizontalListPicProductPanel.MAX){
			list=new ArrayList<Product>(list.subList(0, HorizontalListPicProductPanel.MAX));
		}
		this.targetPage=targetPage;
		
		renderPanel();
	}
	public HorizontalListPicProductPanel(String id,Integer type,String title,Class<? extends WebPage> targetPage) {
		super(id);
		// TODO Auto-generated constructor stub
		this.list=this.service.getProductByType(type);
		
		this.targetPage=targetPage;
		
		this.title=title;
		
		renderPanel();
	}

	private void renderPanel() {
		// TODO Auto-generated method stub

		
		ListView<Product> listView=new ListView<Product>("wk_loop",list) {
			
			@Override
			protected void populateItem(ListItem<Product> arg0) {
				// TODO Auto-generated method stub
				Product product=arg0.getModelObject();
				arg0.add(new SingleSmallPicProductPanel("wk_product",product,targetPage));
			}
		};
		add(listView);
		
		add(new Label("wk_title",title){{
			if(title.equals("DEFAULT")){
				this.setVisible(false);
			}
		}});
	}

	public HorizontalList4PicProductPanelService getService() {
		return service;
	}

	public void setService(HorizontalList4PicProductPanelService service) {
		this.service = service;
	}

	

}
